package ogsadai;

//(c) International Business Machines Corporation, 2005.
//(c) University of Edinburgh, 2005.
//See OGSA-DAI-Licence.txt for licencing information.

//package uk.org.ogsadai.examples.clienttoolkit;

import uk.org.ogsadai.client.databrowser.commands.SQLQueryCommand;
import uk.org.ogsadai.client.toolkit.GenericServiceFetcher;
import uk.org.ogsadai.client.toolkit.Response;
import uk.org.ogsadai.client.toolkit.activity.ActivityRequest;
import uk.org.ogsadai.client.toolkit.activity.sql.SQLQuery;
import uk.org.ogsadai.client.toolkit.activity.sql.WebRowSet;
import uk.org.ogsadai.client.toolkit.service.DataService;

/**
* A simple example of using the client toolkit to run an SQL
* query and return the results in the response to the client
* as WebRowSet.
*
* @author The OGSA-DAI team.
*/
public class SimpleExample {

  // Copyright statement
  private static final String COPYRIGHT_NOTICE =
      "(c) International Business Machines Corporation, 2005. (c) University of Edinburgh 2002 - 2005.";

  
  public String getQuery(String handle, String id, String sql) throws Exception {
	  //String handle = "http://localhost:8080/wsrf/services/ogsadai/MyDataService";
	  //String id = "pharma1_rid";


	  DataService service = GenericServiceFetcher.getInstance().getDataService(handle, id);
	  SQLQuery query = new SQLQuery(sql);
	  WebRowSet rowset = new WebRowSet( query.getOutput() );
	  ActivityRequest request = new ActivityRequest();
	  request.add( query );
	  request.add( rowset );
	  Response response = service.perform( request );
	  return response.getAsString();

  }
  public static void main(String[] args) throws Exception {
	SimpleExample se1=new SimpleExample ();
     
      
	  String handle = "http://localhost:8080/wsrf/services/ogsadai/MyDataService";
      String id = "pharma1_rid";
      String sql="select * from table1 where id=123";
      
      
      System.out.println("\nSQL1 query : "+ se1.getQuery(handle, id, sql));
      
      
      SimpleExample se2=new SimpleExample ();
      //String handle = "http://localhost:8080/wsrf/services/ogsadai/MyDataService";
      String id2 = "pharma2_rid";
      String sql2="select * from table2 ";
      
      System.out.println("\nSQL2 query : "+ se2.getQuery(handle, id2, sql2));
      
      
  }
}
